package com.platform.controller.tenant;

import com.platform.common.annotation.Log;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.BusinessType;
import com.platform.common.enums.ResultEnum;
import com.platform.common.exception.BusinessException;
import com.platform.system.domain.dto.TenantDto;
import com.platform.system.domain.vo.chip.TenantVo;
import com.platform.system.service.ISysTenantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 租户
 *
 * @author Huangkai
 */
@Slf4j
@RestController
@RequestMapping("/tenant/tenantEnter")
public class TenantEnterController {

    @Autowired
    ISysTenantService sysTenantService;

    /**
     * 查询租户列表
     */
    @PreAuthorize("@ss.hasPermi('tenant:tenantEnter:list')")
    @Log(title = "查询租户列表", businessType = BusinessType.OTHER)
    @GetMapping("/selectTenantList")
    public TableDataInfo selectTenantList(TenantVo tenantVo) {
        return sysTenantService.selectTenantList(tenantVo);
    }

    /**
     * 查询租户列表
     */
    @PreAuthorize("@ss.hasPermi('tenant:tenantEnter:query')")
    @Log(title = "查询租户详情", businessType = BusinessType.OTHER)
    @GetMapping(value = "/selectTenant/{uid}")
    public AjaxResult selectTenant(@PathVariable(value = "uid") String uid) {
        TenantVo tenantVo = new TenantVo();
        tenantVo.setUid(uid);
        TenantDto tenantDto = sysTenantService.selectTenant(tenantVo);
        if (tenantDto == null) {
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        return AjaxResult.success(tenantDto);
    }

    /**
     * 插入租户
     */
    @PreAuthorize("@ss.hasPermi('tenant:tenantEnter:add')")
    @Log(title = "插入租户", businessType = BusinessType.INSERT)
    @PostMapping("/insertTenant")
    public AjaxResult insertTenant(@Validated @RequestBody TenantVo tenantVo) {
        return sysTenantService.insertTenant(tenantVo);
    }

    /**
     * 更新租户
     */
    @PreAuthorize("@ss.hasPermi('tenant:tenantEnter:edit')")
    @Log(title = "更新租户", businessType = BusinessType.UPDATE)
    @PutMapping("/updateTenant")
    public AjaxResult updateTenant(@Validated @RequestBody TenantVo tenantVo) {
        return sysTenantService.updateTenant(tenantVo);
    }

    /**
     * 审批租户入驻
     */
    @PreAuthorize("@ss.hasPermi('tenant:tenantEnter:approve')")
    @Log(title = "审批租户入驻", businessType = BusinessType.UPDATE)
    @PutMapping("/approveTenant")
    public AjaxResult approveTenant(@Validated @RequestBody TenantVo tenantVo) {
        return sysTenantService.approveTenant(tenantVo);
    }

    /**
     * 删除租户
     */
    @PreAuthorize("@ss.hasPermi('tenant:tenantEnter:remove')")
    @Log(title = "删除租户", businessType = BusinessType.DELETE)
    @PostMapping("/deleteTenant")
    public AjaxResult deleteTenant(@Validated @RequestBody TenantVo tenantVo) {
        return sysTenantService.deleteTenant(tenantVo);
    }
}
